

package com.hazelcast.test.starter.answer;

import com.hazelcast.map.impl.recordstore.RecordStore;
import org.mockito.invocation.InvocationOnMock;

import java.lang.reflect.Method;

import static org.mockito.Mockito.mock;

/**
 * Default {@link org.mockito.stubbing.Answer} to create a mock for a proxied
 * {@link com.hazelcast.map.impl.PartitionContainer}
 * or {@link com.hazelcast.multimap.impl.MultiMapPartitionContainer}.
 */
class PartitionContainerAnswer extends AbstractAnswer {

    PartitionContainerAnswer(Object delegate) {
        super(delegate);
    }

    @Override
    Object answer(InvocationOnMock invocation, String methodName, Object[] arguments) throws Exception {
        if (arguments.length == 1 && methodName.equals("getExistingRecordStore")) {
            // PartitionContainer
            return getRecordStore(methodName, arguments);
        } else
            if (arguments.length == 2 && methodName.equals("getMultiMapContainer")) {
                // MultiMapPartitionContainer
                return invoke(invocation, arguments);
            } else
                if (arguments.length == 0) {
                    return invoke(invocation);
                }
        throw new UnsupportedOperationException("Method is not implemented in PartitionContainerAnswer: " + methodName);
    }

    private Object getRecordStore(String methodName, Object[] arguments) throws Exception {
        Method delegateMethod = getDelegateMethod(methodName, String.class);
        Object recordStore = delegateMethod.invoke(delegate, arguments);
        if (recordStore == null) {
            return null;
        }
        return mock(RecordStore.class, new RecordStoreAnswer(recordStore));
    }
}
